// pages/orderAddress/orderAddress.js
const addressService = require('../../service/addressService.js')
const utils = require('../../utils/util.js');
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: '3UJBZ-JK6W5-HHLIQ-QOF7B-CQ4U7-D3FJZ' // 必填
});
// load promise 
require('../../utils/wx-pro.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userAddressList: [],
    shopId: '',
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      shopId: options.shopId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddressList()
  },

  getUserAddressList: function () {
    var params = {
      shopId: this.data.shopId
    }
    addressService.listXcxAddressByShop(params).then(data => {
      if (data.code == 200) {
        console.log(data.response.xcxAddressList)
        this.setData({
          userAddressList: data.response.xcxAddressList
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch(() => {
        console.log('error')
      })
  },

  addressLink() {
    var that = this
    wx.chooseAddress({
      success: function (res) {
        console.log(res)
        that.map(res)
      }
    })
  },

  map: function (data) {
    var that = this;
    demo.geocoder({
      address: data.provinceName + data.cityName + data.countyName + data.detailInfo,
      success: function (res) {
        var params = {
          provinceId: data.nationalCode.substr(0, 2) + '0000',
          cityId: data.nationalCode.substr(0, 4) + '00',
          districtId: data.nationalCode,
          detailAddress: data.provinceName + data.cityName + data.countyName + data.detailInfo,
          longitude: res.result.location.lng,
          latitude: res.result.location.lat,
          contactName: data.userName,
          contactPhone: data.telNumber
        }
        addressService.saveXcxAddress(params).then(data => {
          if (data.code == 200) {
            console.log('已保存')
            that.getUserAddressList()
          } else {
            that.showToastFuc(data.message)
          }
        })
          .catch(() => {
            console.log('error')
          })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },

  selectThisAddress(event) {
    var _this = this
    console.log(event.currentTarget.dataset.info)
    var dataMore = event.currentTarget.dataset.info
    wx.setStorageSync('IS_EXPRESS', 'true')
    wx.setStorageSync('selectAddressInfo', JSON.stringify(dataMore))
    addressService.getShopById({ shopId: _this.data.shopId}).then(data => {
      if (data.code == 200) {
        var info = data.response.shop
        var shopInfo = {}
        for (var i in info){
          if(i == 'shopId'){
            shopInfo.id = info[i]
          }else if(i == 'shopName'){
            shopInfo.name = info[i]
          }
        }
        wx.setStorageSync('selectShopInfo', JSON.stringify(shopInfo))
        console.log(shopInfo)
      } else {
        _this.showToastFuc(data.message)
      }
    })
      .catch((e) => {
        console.log(e)
      })
    wx.navigateBack({})
  },
  backIndex(){
    wx.showModal({
      title: '提示',
      content: '是否放弃此订单重新选址？',
      confirmText: '是',
      cancelText: '否',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorageSync('selectShopInfo')
          wx.switchTab({
            url: '../index/index'
          })
        }
      }
    })
  }
})